﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

public partial class manager_user : System.Web.UI.Page
{
    private Connection myConnection = null;
    private SQLQuery sqlQuery = null;
    private string TABLE_EXAMCODE = "ExamCode";
    private string TABLE_EXAM = "Exam";
    private string TABLE_QUESTIONS = "Questions";
    private string txtExamCode, txtExamType;
    private int ADD_QUES_LIMIT = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"] == null)
        {
            Response.Redirect("../index.htm");
        }
        myConnection = new Connection();
        if (!myConnection.isConnectionDatabase())
        {
            Response.Redirect("../disconnect.htm");
        }

        sqlQuery = new SQLQuery();
        if (!IsPostBack)
        {           
            fillDataToGridView();
            showGridView();
        } 
    }

    private void showGridView()
    {
        layout_gridview.Visible = true;
        layout_infor.Visible = false;
    }

    private void showLayoutInfor()
    {
        layout_gridview.Visible = false;
        layout_infor.Visible = true;
    }

    private void fillDataToGridView()
    {
        sqlQuery.loadDataGridView(mGridView, LocalVariables.SQLQUERY_SELECT_TABLE_EXAM_CODE);
    }

    private bool getText()
    {
        lbl_msg.Text = "";
        txtExamCode = txt_examcode.Text.Trim();
        if (LocalVariables.isNullorEmpty(txtExamCode))
        {
            lbl_msg.Text = LocalVariables.EXAMCODE_FAILURE;
            txt_examcode.Focus();
            return false;
        }
        if (ddl_type.SelectedIndex == 0)
        {
            lbl_msg.Text = LocalVariables.CHOOSE_EXAM_TYPE;
            return false;
        }
        else
        {
            txtExamType = ddl_type.Text.Trim();
        }
        return true;
    }

    private void clearValue()
    {
        lbl_msg.Text = "";
        txt_examcode.Text = "";
        ddl_type.SelectedIndex = 0;
    }

    protected void mGridView_SelectedIndexChanged(object sender, EventArgs e)
    {
        lbl_msg.Text = "";
        lbl_add_ques.Text = "";        
        hdf_value.Value = mGridView.SelectedRow.Cells[1].Text.Trim();
        txtExamCode = mGridView.SelectedRow.Cells[1].Text.Trim();
        lbl_examcode.Text = "Exam code : " + mGridView.SelectedRow.Cells[1].Text.Trim();
        List<string> list = sqlQuery.fillAllQuestion(mGridView.SelectedRow.Cells[1].Text.Trim());
        if (list != null)
        {
            sqlQuery.fillExam(mGridView_ViewQuestion, list);
            showLayoutInfor();
        }
        else
        {
            lbl_msg.Text = "Question \" " + mGridView.SelectedRow.Cells[2].Text.Trim() + " \" not in the library";
        }
    }

    protected void mGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        mGridView.PageIndex = e.NewPageIndex;
        fillDataToGridView();
    }

    protected void btn_add_Click(object sender, EventArgs e)
    {        
        if (getText())
        {
            List<string> list = sqlQuery.selectAllQuestion(txtExamType);
            if (list != null)
            {
                int[] ran = randomNumberQuestion(list.Count);
                if (ran != null)
                {
                    string examCode = txtExamCode.ToUpper().Trim();
                    if (sqlQuery.insertTableExam(TABLE_EXAMCODE, examCode, txtExamType))
                    {
                        for (int i = 0; i < ran.Length; i++)
                        {
                            sqlQuery.insertTableExam(TABLE_EXAM, examCode, list[ran[i]]);
                        }
                        clearValue();
                        lbl_msg.Text = LocalVariables.ADD_EXAM_SUCCESS;
                        fillDataToGridView();
                    }                    
                }
                else
                {
                    lbl_msg.Text = LocalVariables.EXAM_TYPE_NOT_QUESTION;
                }                
            }
            else
            {
                lbl_msg.Text = LocalVariables.EXAM_TYPE_NOT_QUESTION;
            }
        }
    }

    private int[] randomNumberQuestion(int size)
    {
        if (size < 10)
        {
            return null;
        }
        Random rand = new Random();
        int[] array = new int[] { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };

        bool ran = false;
        for (int i = 0; i < 10; i++)
        {
            ran = false;
            while (!ran)
            {
                int number = rand.Next(size);
                int j = 0;
                for (j = 0; j < array.Length; j++)
                {
                    if (number == array[j])
                    {
                        j--;
                        break;
                    }
                }
                if (j == array.Length)
                {
                    array[i] = number;
                    ran = true;
                }
            }
        }
        return array;
    }

    protected void btn_refresh_Click(object sender, EventArgs e)
    {
        fillDataToGridView();
        clearValue();        
    }

    protected void btn_del_Click(object sender, EventArgs e)
    {
        try
        {
            HtmlInputCheckBox chk = new HtmlInputCheckBox();
            for (int i = 0; i < mGridView.Rows.Count; i++)
            {
                chk = (HtmlInputCheckBox)mGridView.Rows[i].FindControl("chk");
                if (chk.Checked == true)
                {
                    sqlQuery.deleteTable(TABLE_EXAMCODE, "[examCode]", chk.Value.ToString());
                }
            }
            fillDataToGridView();
        }
        catch { }
    }

    protected void btn_back_Click(object sender, EventArgs e)
    {
        showGridView();
    }   
}